#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode {
    int val;
    struct ListNode* next;
}LN;

struct ListNode* reverseList(struct ListNode* head)
{
    struct ListNode* n1 = NULL;
    struct ListNode* n2 = head;
    struct ListNode* n3 = NULL;
    if (head != NULL)
        n3 = head->next;
    while (n3 != NULL)
    {
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        n3 = n3->next;
    }
    if (head != NULL)
        n2->next = n1;
    return n2;
}

int main()
{
    LN ln5 = { 5,NULL };
    LN ln4 = { 4,&ln5 };
    LN ln3 = { 3,&ln4 };
    LN ln2 = { 2,&ln3 };
    LN ln1 = { 1,&ln2 };
    LN* print = &ln1;
    while (print != NULL)
    {
        printf("%d ", print->val);
        print = print->next;
    }
    printf("\n");
    print = reverseList(&ln1);
    while (print != NULL)
    {
        printf("%d ", print->val);
        print = print->next;
    }
    return 0;
}